home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / full / CBUILDER / SAMS / SAMPLES / CHAP03 / REFERENC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-12  |  2.2 KB  |  96 lines

  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #pragma hdrstop
  5.  
  6. #include "structur.h"
  7.  
  8. void displayRecord(int, mailingListRecord mlRec);
  9.  
  10. int main(int, char**)
  11. {
  12.   cout << endl;
  13.   //
  14.   // create an array of mailingListRecord structures
  15.   //
  16.   mailingListRecord* listArray[3];
  17.   //
  18.   // create objects for each record
  19.   //
  20.   for (int i=0;i<3;i++)
  21.     listArray[i] = new mailingListRecord;
  22.   int index = 0;
  23.   //
  24.   // get three records
  25.   //
  26.   do {
  27.     // create a reference to the current record
  28.     mailingListRecord& rec = *listArray[index];
  29.     cout << "First Name: ";
  30.     cin.getline(rec.firstName, sizeof(rec.firstName) - 1);
  31.     cout << "Last Name: ";
  32.     cin.getline(rec.lastName, sizeof(rec.lastName) - 1);
  33.     cout << "Address: ";
  34.     cin.getline(rec.address, sizeof(rec.address) - 1);
  35.     cout << "City: ";
  36.     cin.getline(rec.city, sizeof(rec.city) - 1);
  37.     cout << "State: ";
  38.     cin.getline(rec.state, sizeof(rec.state) - 1);
  39.     char buff[10];
  40.     cout << "Zip: ";
  41.     cin.getline(buff, sizeof(buff) - 1);
  42.     rec.zip = atoi(buff);
  43.     index++;
  44.     cout << endl;
  45.   }
  46.   while (index < 3);
  47.   //
  48.   // display the three records
  49.   //
  50.   clrscr();
  51.   //
  52.   // must dereference the pointer to pass an object
  53.   // to the displayRecord function.
  54.   //
  55.   for (int i=0;i<3;i++) {
  56.     displayRecord(i, *listArray[i]);
  57.   }
  58.   //
  59.   // ask the user to choose a record
  60.   //
  61.   cout << "Choose a record: ";
  62.   char rec;
  63. do {
  64.     rec = getch();
  65.     rec -= 49;
  66.   } while (rec < 0 || rec > 2);
  67.   //
  68.   // assign the selected record to a temporary variable
  69.   // must dereference here, too
  70.   //
  71.   mailingListRecord temp = *listArray[rec];
  72.   clrscr();
  73.   cout << endl;
  74.   //
  75.   // display the selected recrord
  76.   //
  77.   displayRecord(rec, temp);
  78.   getch();
  79.   return 0;
  80. }
  81.  
  82. void displayRecord(int num, mailingListRecord mlRec)
  83. {
  84.   cout << "Record " << num + 1 << ":" << endl;
  85.   cout << "Name:     " << mlRec.firstName << " ";
  86.   cout << mlRec.lastName;
  87.   cout << endl;
  88.   cout << "Address:  " << mlRec.address;
  89.   cout << endl << "          ";
  90.   cout << mlRec.city << ", ";
  91.   cout << mlRec.state << "  ";
  92.   cout << mlRec.zip;
  93.   cout << endl << endl;
  94. }
  95.  
  96.